package com.jye.rseven.user.controller;




import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.*;
import com.jye.rseven.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/follow")
@Api(tags="关注控制器API")
public class FollowController {

    @Autowired
    FollowService followService;

    @Autowired
    CoursesService coursesService;

    @Autowired
    TaskService taskService;

    @Autowired
    UserService userService;

    @Autowired
    ActivitiesService activitiesService;

    @PostMapping(value="/doTaskFollow")
    @ApiOperation(value="关注打卡任务", notes="需要3个参数,用户id:uId,父id:pId,状态:status", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "follow",
                    value = "follow实体类", required = true,
                    dataType = "Integer"),
    })
    public JsonResult doTaskFollow(Follow follow){
        JsonResult jsonResult = new JsonResult();
        //打卡类别
        int taskType = MyConstants.followType.taskType;
        follow.setType(taskType);
        int i = followService.doFollow(follow);
        if(i==1){
            jsonResult.setMsg("关注成功!");
        }else {
            jsonResult.setMsg("关注失败!");
        }
        return jsonResult;
    }


    @PostMapping(value="/doCoursesFollow")
    @ApiOperation(value="关注课程", notes="需要3个参数,用户id:uId,父id:pId,状态:status", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "follow",
                    value = "follow实体类", required = true,
                    dataType = "Integer"),
    })
    public JsonResult doCoursesFollow(Follow follow){
        JsonResult jsonResult = new JsonResult();
        //课程类别
        int taskType = MyConstants.followType.courseType;
        follow.setType(taskType);
        int i = followService.doFollow(follow);
        if(i==1){
            jsonResult.setMsg("关注成功!");
        }else {
            jsonResult.setMsg("关注失败!");
        }
        return jsonResult;
    }

    @PostMapping(value="/doActivitiesFollow")
    @ApiOperation(value="关注活动", notes="需要3个参数,用户id:uId,父id:pId,状态:status", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "follow",
                    value = "follow实体类", required = true,
                    dataType = "Integer"),
    })
    public JsonResult doActivitiesFollow(Follow follow){
        JsonResult jsonResult = new JsonResult();
        //课程类别
        int activities = MyConstants.followType.activitiesType;
        follow.setType(activities);
        int i = followService.doFollow(follow);
        if(i==1){
            jsonResult.setMsg("关注成功!");
        }else {
            jsonResult.setMsg("关注失败!");
        }
        return jsonResult;
    }

    @GetMapping(value="/findMyFollowCoursesByUId")
    @ApiOperation(value="查找我的关注课程", notes="", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyFollowCoursesByUId(int uId){
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.followType.courseType;
        //查找课程ids

        List<Integer> ids = followService.findMyFollowByUIdAndType(uId,type);
        //变量
        List<Courses> coursesList = new ArrayList<>();
        Courses course;
        //遍历
        for (Integer id : ids) {
            course= coursesService.findCourseById(id);
            coursesList.add(course);
        }
        if(coursesList.size()!=0){
            jsonResult.setData(coursesList);
        }else{
            jsonResult.setMsg("暂无查询到你关注的课程!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping(value="/findMyFollowTasksByUId")
    @ApiOperation(value="查找我的关注打卡", notes="", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyFollowTasksByUId(int uId){
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.followType.taskType;
        //查找打卡ids
        List<Integer> ids = followService.findMyFollowByUIdAndType(uId,type);
        //变量
        List<Task> taskList = new ArrayList<>();
        Task task;
        //遍历
        for (Integer id : ids) {
            task= taskService.findTaskById(id);
            task.setIsfollow(true);
            task.setUser(userService.findUserById(task.getUId()));
            taskList.add(task);
        }
        if(taskList.size()!=0){
            jsonResult.setData(taskList);
        }else{
            jsonResult.setMsg("暂无查询到你关注的打卡!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping(value="/findMyFollowActivitiesByUId")
    @ApiOperation(value="查找我的关注活动", notes="", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyFollowActivitiesByUId(int uId){
        JsonResult jsonResult = new JsonResult();
        int type = MyConstants.followType.activitiesType;
        //查找活动ids
        List<Integer> ids = followService.findMyFollowByUIdAndType(uId,type);
        //变量
        List<Activities> activitiesList = new ArrayList<>();
        Activities activities;
        //遍历
        for (Integer id : ids) {
           activities = activitiesService.findActivitiesById(id);
           activitiesList.add(activities);
        }
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else{
            jsonResult.setMsg("暂无查询到你关注的活动!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }




}
